package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.drugtag.DrugTagAddReqDTO;
import com.yixin.hospital.entity.dto.req.drugtag.DrugTagEditReqDTO;
import com.yixin.hospital.service.DrugTagService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/drug-tag")
@Api(value = "药品标签管理", tags = "药品标签管理")
public class DrugTagController {

    @Resource
    private DrugTagService drugTagService;

    @GetMapping("/list")
    @ApiOperation(value = "药品标签列表")
    public ApiResult<?> list() {

        return ApiResult.success(drugTagService.list());
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加药品标签")
    public ApiResult<?> add(@Valid @RequestBody DrugTagAddReqDTO drugTagAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugTagService.add(drugTagAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑药品类别")
    public ApiResult<?> edit(@Valid @RequestBody DrugTagEditReqDTO drugTagEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugTagService.edit(drugTagEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除药品标签")
    public ApiResult<?> remove(@PathVariable int id) {
        drugTagService.removeById(id);
        return ApiResult.success();
    }
}
